<?php

/**
 * add by allen.qiang 2015/12/8
 * 接口控制器
 * 用于分配不同业务类进行处理
 */

require ('core/shop.php');

//定义业务列表****************************************
$service_list = array('bill', 'customer', 'goods');

//检查参数********************************************
if (isset($_REQUEST['service']) && !in_array($_REQUEST['service'], $service_list)){
    error_log('service参数错误：' . $_REQUEST['service']);
    die( json_encode( array( 'message' => 'Parameter_Error', 'code' => 501 ) ) );
}
//检查参数********************************************
if (!isset($_REQUEST['token'])){
    error_log('token参数错误：' . $_REQUEST['token']);
    die( json_encode( array( 'message' => 'Parameter_Error', 'code' => 501 ) ) );
}

//签名验证********************************************
$para_string = '';
foreach($_REQUEST as $key => $value ) {
    if($key !='token')
        $para_string = $para_string .$value;
}

if(md5($para_string) != $_REQUEST['token']){
    error_log('token验证失败：' . $_REQUEST['token']);
    die( json_encode( array( 'message' => 'Incorrect_Token', 'code' => 503 ) ) );
}

$service = $_REQUEST['service'];

//查询ajax类文件
$action_file_name = G_APPLICATION_BASEPATH . sprintf('if/app/%s/app_%s.php', $service, $service);
if (! is_file($action_file_name)) {
    error_log('文件不存在：' . $action_file_name);
    die( json_encode( array( 'message' => 'System_error', 'code' => 900 ) ) );
}

//加载class文件
quanli_require( $action_file_name );

//实例化class类
$action_class_name = 'app_' . $service;

$app = new $action_class_name ();

header("Content-type:text/html;charset=utf-8");
$app->execute ();
